Right Shift Operator (>>) Examples
Consider the binary number 1101 0010
, which is 210
in decimal. If we right-shift it by 2 positions:
1101 0010 >> 2
The result would be 0011 0100
, which is 52
in decimal. This is because each bit is shifted two positions to the right, and the vacant positions on the left are filled with 0
s.
#include <iostream>
using namespace std;
int main()
{
// Binary representation: 11010010
int num = 210;
int shifted_num = num >> 2;
cout << shifted_num << endl;
return 0;
}
#include <stdio.h>
int main() {
// Binary representation: 11010010
int num = 210;
int shifted_num = num >> 2;
printf("%d\n", shifted_num);
return 0;
}
/* package whatever // do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args) {
// Binary representation: 11010010
int num = 210;
int shiftedNum = num >> 2;
System.out.println(shiftedNum);
}
}
# Binary representation: 11010010
num = 210
shifted_num = num >> 2
print(shifted_num)
using System;
class Program {
static void Main()
{
// Binary representation: 11010010
int num = 210;
int shiftedNum = num >>2;
Console.WriteLine(shiftedNum);
}
}
// Binary representation: 11010010
let num = 210;
let shiftedNum = num>>2;
console.log(shiftedNum);
Output
52
Right Shift Operator (>>) in Programming
Right shift operator (>>), commonly found in programming languages, including C, C++, Java, and others, is used to shift the bits of a number to the right by a specified number of positions. Each shift moves all bits in the operand to the right by the number of positions indicated by the right operand.
Table of Content
- Right Shift Operator (>>) Definition
- Right Shift Operator (>>) Syntax
- Right Shift Operator (>>) Examples
- Right Shift Operator (>>) with Signed Integers
- Right Shift Operator (>>) with Unsigned Integers
- Right Shift Operator (>>) with Signed vs. Unsigned Integers
- Logical Right Shift
- Arithmetic Right Shift
- Logical Right Shift vs. Arithmetic Right Shift
- Right Shift Operator (>>) Optimization Techniques
- Bit Manipulation Hacks with Right Shift Operator
This comprehensive guide aims to provide a deep understanding of bitwise right shift operators, from the foundational principles to advanced optimization strategies.
Contact Us